 
import java.util.ArrayList;
import java.io.*;

/**
 * Write a description of class KundeCont here.
 * 
 * @author (Kasper) 
 * @version (05.12.2014)
 */
public class KundeCont
{
    private ArrayList<Kunde> kunder;
    private PersonFileHandler fileHandler;
    private static KundeCont instance;
    
    /**
     * Constructor for objects of class KundeCont
     */
    public KundeCont()
    {
        kunder = new ArrayList<Kunde>();
        fileHandler = new PersonFileHandler("kundeData");
    }
    
    public ArrayList<Kunde> getList()
    {
        return kunder;
    }

    /**
     * Singleton Method getInstance
     *
     *Creates a KundeCont if one does not exist
     *
     * @return instance
     */
    public static KundeCont getInstance()
    {
        if(instance == null)
        {
            instance = new KundeCont();
        }
        return instance;
    }

    /**
     * Method addKunde
     *
     * adds a customer K to the arraylist of customers
     *
     * @param k A customer
     */
    public void addKunde(Kunde k)
    {
        kunder.add(k);
    }

    /**
     * Method findKunde
     *
     * Searches for a customer with the given phone number
     * 
     * @param tlf Telephone number
     * @return A customer of the type Kunde
     */
    public Kunde findKunde(String tlf)
    {
        Kunde returnKunde = null;
        boolean found = false;
        int size = kunder.size();
        for(int i = 0; i < size && !found; i++)
        {
            if(kunder.get(i).getTlf().equals(tlf))
            {
                returnKunde = kunder.get(i);
                found = true;
            }
        }
        return returnKunde;
    }

    /**
     * Method deleteKunde
     *
     * Removes a customer with the given phone number. Returns a boolean on wether or not
     * the person was removed.
     * 
     * @param tlf Telephone number
     * @return False if the customer couldn't be found. True if the customer was removed.
     */
    public boolean deleteKunde(String tlf)
    {
        boolean success = true;
        if(findKunde(tlf) == null)
        {
            success = false;
        }
        Kunde k = findKunde(tlf);
        kunder.remove(k);

        return success;
    }

    public Kunde findKundeOrdreNr(int ordreNr)
    {
        Kunde returnKunde = null;
        int i = 0;
        int j = 0;
        boolean found = false;
        while(!found && i < kunder.size())
        {
            while(!found && j < kunder.get(i).getOList().size())
            {
                if(kunder.get(i).getOList().get(j).getOrdreNr() == ordreNr)
                {
                    returnKunde = kunder.get(i);
                    found = true;
                }
                else
                {
                    j++;
                }
            }    
            j = 0;
            i++;
        }
        return returnKunde;
    }

    /**
     * Method updateKunde
     *
     * Updates all given parameters of a customer
     * 
     * @param oldTlf A parameter
     * @param navn A parameter
     * @param adresse A parameter
     * @param postnr A parameter
     * @param by A parameter
     * @param tlf A parameter
     * @param email A parameter
     * @param kontonr A parameter
     * @param kundegrp A parameter
     * @return True if the ipdate succeded. False if not.
     */
    public boolean updateKunde(String oldTlf, String navn, String adresse, String postnr, String by, String tlf, String email, int kontonr, int kundegrp)
    {
        boolean success = false;
        Kunde k = findKunde(oldTlf);
        k.setNavn(navn);
        k.setAdresse(adresse);
        k.setPostnr(postnr);
        k.setBy(by);
        k.setTlf(tlf);
        k.setEmail(email);
        if(k instanceof FastKunde)
        {
            FastKunde fastekunder = (FastKunde) k;
            fastekunder.setKontonr(kontonr);
            fastekunder.setKundegrp(kundegrp);
            if(k.getNavn().equals(navn) && k.getAdresse().equals(adresse) && k.getPostnr().equals(postnr) && k.getBy().equals(by) && k.getTlf().equals(tlf) && k.getEmail().equals(email) && fastekunder.getKontonr() == kontonr && fastekunder.getKundegrp() == kundegrp)
            {
                success = true;
            }
        }

        return success;
    }

    public void saveToFile() throws IOException
    {
        fileHandler.saveToFileKunder(kunder);
    }

    public void loadFromFile()
    throws IOException, ClassNotFoundException
    {
        kunder = fileHandler.readFromFileKunde();
    }

    /**
     * Method printInfo
     *
     * Prints info about customers.
     */
    public void printInfo()
    {
        for(int i = 0; i < kunder.size(); i++)
        {
            System.out.println(kunder.get(i));
        }
    }
}
